﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ionic.Zip;
using System.IO;

namespace Common.FileHandling
{
    /// <summary>
    /// Author: Mike Barksdale
    /// Utility class for zipping a set of files. Uses the Ionic.Zip assembly.
    /// </summary>
    public class FileHandler
    {
        public byte[] ZippedFile(List<SaveFile> filesToZip)
        {
            //create a zip file containing both files (uses the Ionic.Zip assembly)
            using (var zip = new ZipFile())
            {
                //add the entries
                foreach (var file in filesToZip)
                {
                    zip.AddEntry(file.fileName, file.contents);
                }
                //grab the zipped file from memory and return it
                using (var ms = new MemoryStream())
                {
                    zip.Save(ms);
                    return ms.ToArray();
                }
            }
        }
    }

    /// <summary>
    /// Author: Mike Barksdale
    /// Simple class to store the name and contents of a file. Used in conjunction with FileHandler.
    /// </summary>
    public class SaveFile
    {
        public string fileName { get; set; }
        public byte[] contents { get; set; }
    }
}